<template>
  <div class="modal-form-container">
    <Form ref="info" :model="menuData" :rules="rules" :label-width="100">
      <FormItem :label="$t('HfDg9OexYXAbDV3inAO6R')" prop="name">
        <Select v-if="status === 'new'" v-model="menuData.id" @on-change="changeMenu">
          <Option
            v-for="menu in menuLists"
            :key="menu.id"
            :value="menu.id"
            >{{ menu.titlename }} ({{ menu.name }})</Option
          >
        </Select>
        <Input v-else disabled v-model="menuData.name" />
      </FormItem>
      <FormItem :label="$t('1m5Mrf5otrghObt6WEnHU')">
        <Row>
          <Col span="22">
            <Input disabled v-model="menuData.urlname" />
          </Col>
          <Col offset="1" span="1">
            <el-tooltip
              effect="dark"
              :content="$t('pTmYORL8z7HdqrV0wAXqP')"
            >
              <i class="el-icon-info"></i>
            </el-tooltip>
          </Col>
        </Row>
      </FormItem>
      <FormItem :label="$t('vzJyP1frPrz_UYCIF603L')" prop="titlename">
        <Input disabled v-model="menuData.titlename" />
      </FormItem>
      <FormItem v-if="parent && parent.name" :label="$t('lwjOeFVMnIEr-OdlXnwxY')" prop="titlename">
        <Input disabled v-model="parent.titlename" />
      </FormItem>
      <FormItem :label="$t('BanFOXoRcUwxeJvr8DTNW')" prop="sort">
        <Input disabled v-model="menuData.sort" />
      </FormItem>
      <FormItem>
        <Button type="primary" @click="save">{{$t('q-FETLOZVSMbWnrNq0vj6')}}</Button>
        <Button style="margin-left: 8px" @click="close">{{$t('2LFpg56tAgKyBB40xpv5l')}}</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>

const isFalse = p => {
  return _.isNil(p) || p === '' || p == 0
}

export default {
  name: 'rolemenu-form',
  props: {
    data: Object,
    menulist: Array,
    rules: Object,
    status: String,
    show: Boolean,
    parent: Object
  },
  data () {
    return {
      menuData: _.cloneDeep(this.data),
      menuLists: []
    }
  },
  created () {
    this.getMenuLists()
  },
  watch: {
    show (val) {
      if (val) {
        this.menuData = _.cloneDeep(this.data)
        this.getMenuLists()
      }
    }
  },
  methods: {
    getMenuLists () {
      if (this.status === 'new') {
        if (isFalse(this.menuData.pid)) {
          this.menuLists = this.menulist.filter(m => isFalse(m.pid))
        } else {
          this.menuLists = this.menulist.filter(m => m.pid == this.menuData.pid)
        }
      }
    },
    changeMenu (val) {
      console.log(val)
      if (!_.isNil(val)) {
        this.menuData = this.menulist.find(m => m.id == this.menuData.id)
      }
    },
    save () {
      this.$refs.info.validate(async valid => {
        if (valid) {
          this.$emit('save', { ...this.menuData }, this.status)
        } else {
          this.$Message.error(this.$t('fcmS3YBVNyvO3V68Qt3xl'))
        }
      })
    },
    close () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="stylus" scoped>
.modal-form-container
  height auto
</style>
